<!--
 * @Author: sunday
 * @Date: 2020-07-15 09:11:21
 * @LastEditors: sunday
 * @LastEditTime: 2020-07-15 09:19:44
 * @Description: file content
-->
<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>大量数据图表</title>
  <link href="./css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="./css/step.css">
  <link rel="stylesheet" href="./css/index.css">
</head>

<body>

  <div class="container">
    <h1>大量数据图表</h1>
    <div class="row">
      <div class="col-sm-12 col-md-6 ">
        <div class="chart-wrap" style="height: 5rem;">
          <div class="chart text-center" id="bar-1"></div>
        </div>
        <div id="down">下载</div>
      </div>
      <div class="col-sm-12 col-md-6 ">
        <div class="chart-wrap" style="height: 5rem;">
          <div class="chart" id="bar-2"></div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-12 col-md-6 ">
        <div class="chart-wrap" style="height: 5rem;">
          <div class="chart text-center" id="chart_bar_3"></div>
        </div>
        <div class="alert alert-info">dataZoom</div>
      </div>
      <div class="col-sm-12 col-md-6 ">
        <div class="chart-wrap" style="height: 5rem;">
          <div class="chart" id="chart_bar_4"></div>
        </div>
        <div class="alert alert-info">垂直方向滚动（切换数据方法）</div>
      </div>
    </div>
  </div>

  <script src="./js/rem.js"></script>
  <script src="./js/jquery-1.8.3.min.js"></script>
  <script src="./js/echarts.min.js"></script>
  <script src="./js/common.js"></script>
  <script src="./js/S_mock.js"></script>
  <script src="./js/chart_bigdata.js"></script>
  <script>
    $(function () {
      chart_bar_1();
      chart_bar_2();
      chart_bar_3();
      chart_bar_4();
      $('#down').click(function () {
        downloadImpByChart('bar-1','name');
      })
      //chartId：echarts图的id；name：下载图片的名字
      function downloadImpByChart(chartId, name) {
        var url = getFullCanvasDataURL(chartId);
        var link = document.createElement('a');
        link.href = url;
        link.download = name + ".png";
        link.click();
      };

      //将图形转成base64的图片
      function getFullCanvasDataURL(divId) {
        //将第一个画布作为基准。
        var baseCanvas = $("#" + divId).find("canvas").first()[0];
        if (!baseCanvas) {
          return false;
        };
        var width = baseCanvas.width;
        var height = baseCanvas.height;
        var ctx = baseCanvas.getContext("2d");
        //遍历，将后续的画布添加到在第一个上
        $("#" + divId).find("canvas").each(function (i, canvasObj) {
          if (i > 0) {
            var canvasTmp = $(canvasObj)[0];
            ctx.drawImage(canvasTmp, 0, 0, width, height);
          }
        });
        //获取base64位的url
        return baseCanvas.toDataURL();
      }

    })
  </script>
</body>

</html>